import {
  faCheck,
  faChevronRight,
  faCode,
  faCog,
  faColumns,
  faEye,
  faEyeSlash,
  faFileCode,
  faFileExport,
  faFileImage,
  faFileImport,
  faFilter,
  faKey,
  faList,
  faMousePointer,
  faPalette,
  faPlus,
  faProjectDiagram,
  faQuestion,
  faRedoAlt,
  faSearch,
  faStickyNote,
  faSyncAlt,
  faTable,
  faTimes,
  faTree,
  faUndoAlt,
} from '@fortawesome/free-solid-svg-icons';
import {
  mdiAtom,
  mdiCodeBrackets,
  mdiCodeJson,
  mdiDatabase,
  mdiDatabaseExport,
  mdiDatabaseImport,
  mdiDotsVertical,
  mdiFormatLetterCase,
  mdiPalette,
  mdiTableCog,
  mdiVectorLine,
  mdiXml,
} from '@mdi/js';
import { observable } from '@vuerd/lit-observable';

import { IconDefinition } from '@@types/core/icon';
import { RelationshipType } from '@@types/engine/store/relationship.state';

export const createMDI = (name: string, icon: string): IconDefinition => ({
  prefix: 'mdi',
  iconName: name,
  icon: [24, 24, , , icon],
});

const icons = [
  faKey,
  faTable,
  faStickyNote,
  faPlus,
  faTimes,
  faChevronRight,
  faCheck,
  faList,
  faRedoAlt,
  faUndoAlt,
  faSearch,
  faFilter,
  faQuestion,
  faProjectDiagram,
  faFileImage,
  faFileExport,
  faEye,
  faEyeSlash,
  faFileImport,
  faFileCode,
  faCog,
  faMousePointer,
  faCode,
  faSyncAlt,
  faTree,
  faPalette,
  faColumns,
  createMDI('code-json', mdiCodeJson),
  createMDI('database', mdiDatabase),
  createMDI('database-import', mdiDatabaseImport),
  createMDI('database-export', mdiDatabaseExport),
  createMDI('palette', mdiPalette),
  createMDI('format-letter-case', mdiFormatLetterCase),
  createMDI('table-cog', mdiTableCog),
  createMDI('code-brackets', mdiCodeBrackets),
  createMDI('xml', mdiXml),
  createMDI('dots-vertical', mdiDotsVertical),
  createMDI('vector-line', mdiVectorLine),
  createMDI('atom', mdiAtom),
] as IconDefinition[];

const iconMap: Record<string, IconDefinition> = observable({});

const setIconMap = (newIcons: IconDefinition[]) =>
  newIcons.reduce<Record<string, IconDefinition>>((acc, cur) => {
    acc[`${cur.prefix}-${cur.iconName}`] = cur;
    return acc;
  }, iconMap);

setIconMap(icons);

export const getIcon = (
  prefix: string,
  iconName: string
): IconDefinition | undefined => iconMap[`${prefix}-${iconName}`];

export const addIcon = (...newIcons: IconDefinition[]) => {
  icons.push(...newIcons);
  setIconMap(newIcons);
};

const base64Icons: Record<string, string> = {
  ZeroOneN:
    '',
  ZeroOne:
    '',
  ZeroN:
    '',
  OneOnly:
    '',
  OneN: '',
  One: '',
  N: '',
};

export const getBase64Icon = (relationshipType: RelationshipType) =>
  base64Icons[relationshipType];
